package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.PackagingRecordsReq;
import com.ruibang.glass.produce.entity.PackagingRecords;
import com.ruibang.glass.produce.feign.QualityApi;
import com.ruibang.glass.produce.mapper.PackagingRecordsMapper;
import com.ruibang.glass.produce.service.PackagingRecordsService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.seata.common.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-03-19  14:37
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class PackagingRecordsServiceImpl extends ServiceImpl<PackagingRecordsMapper, PackagingRecords> implements PackagingRecordsService {

    @Autowired
    private QualityApi qualityApi;

    @Override
    public IPage<PackagingRecords> pageQuery(PackagingRecordsReq packagingRecordsReq) {
        if (packagingRecordsReq.getStartTime() != null && packagingRecordsReq.getEndTime() != null) {
            packagingRecordsReq.setEndTs(packagingRecordsReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
            packagingRecordsReq.setStartTs(packagingRecordsReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        }
        IPage<PackagingRecords> page = baseMapper.pageQuery(new Page<>(packagingRecordsReq.getPageNo(), packagingRecordsReq.getPageSize()), packagingRecordsReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            //获取热端玻璃ID
            List<PackagingRecords> data = page.getRecords();
            List<String> newIds = data.stream().map(PackagingRecords::getColdGlassId).collect(Collectors.toList());
            ResultBody<Map<String, String>> resultBody = qualityApi.getColdIdMappingHotId(newIds);
            if (resultBody != null && resultBody.getData() != null && resultBody.getData().size() > 0) {
                Map<String, String> map = resultBody.getData();
                for (PackagingRecords packagingRecords : data) {
                    String hotId = map.get(packagingRecords.getColdGlassId());
                    if (StringUtils.isNotBlank(hotId) && hotId.startsWith(Constants.HOT_GLASS_ID_PREFIX)) {
                        packagingRecords.setGlassId(hotId);
                    }
                }
            }
        }
        return page;
    }

    @Override
    public void export(PackagingRecordsReq packagingRecordsReq, HttpServletResponse response) {
        packagingRecordsReq.setPageNo(-1);
        packagingRecordsReq.setPageSize(-1);
        IPage<PackagingRecords> page = this.pageQuery(packagingRecordsReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<PackagingRecords> packagingRecordsList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), PackagingRecords.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("包装生产记录").doWrite(packagingRecordsList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }
}